﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DOTNETGROUP.Data;
using DOTNETGROUP.Entities;
using System.IO;
using System.Text;

public partial class Admin_DanhMuc_DMBaoGia : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCatalog.BindLoaiBaoGia(cboLoaiBaoGia);
            BindCatalog.BindLoaiTepTin(cboLoaiTepTin);
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }
    protected void Bind_Data(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "1=1";
        DataSet dsBookCategories = DataRepository.Provider.ExecuteDataSet("BAOGIA_GetPaged", WhereClause, "Id DESC", PageNo - 1, Pager1.PageSize);
        gridCatalog.DataSource = dsBookCategories.Tables[0];
        gridCatalog.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsBookCategories.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;

    }
    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("DMBaoGia.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            BaoGia entBaoGia = new BaoGia();
            entBaoGia.Id = Convert.ToInt32(H_CatalogId.Value);
            entBaoGia.TenBaoGia = txtBaoGia.Text;
            entBaoGia.IdLoaiBaoGia = Convert.ToInt32(cboLoaiBaoGia.SelectedValue);
            entBaoGia.DuongDan = Common.sPathUpBaoGia + lblFileDinhKem.Text;
            entBaoGia.IdLoaiTepTin = Convert.ToInt32(cboLoaiTepTin.SelectedValue);
            entBaoGia.NgayCapNhat = Common.GetServerDateTime();
            entBaoGia.TrangThai = chkTrangThai.Checked;

            if (entBaoGia.Id == 0)
            {
                entBaoGia.Id = 0;
                updateStatus = DataRepository.BaoGiaProvider.Insert(entBaoGia);
            }
            else
            {
                updateStatus = DataRepository.BaoGiaProvider.Update(entBaoGia);
            }
            if (updateStatus)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.BAOGIA_CAPNHAT_THANHCONG, true);
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
                cmdReset_Click(sender, e);
            }
            else
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.BAOGIA_CAPNHAT_THATBAI, true);
        }
        catch (Exception ex)
        {
            Common.WriteLog("DMBaoGia.aspx.cs", "cmdSave_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.BAOGIA_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        txtBaoGia.Text = "";
        lblFileDinhKem.Text = "";
        cboLoaiBaoGia.SelectedValue = "0";
        cboLoaiTepTin.SelectedValue="0";     
        H_CatalogId.Value = "0";
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton l = (ImageButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
        }
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            try
            {
                H_CatalogId.Value = e.CommandArgument.ToString();
                BaoGia entBaoGia = DataRepository.BaoGiaProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                if (entBaoGia != null)
                {
                    txtBaoGia.Text = entBaoGia.TenBaoGia;
                    lblFileDinhKem.Text = entBaoGia.DuongDan;
                    cboLoaiBaoGia.SelectedValue = entBaoGia.IdLoaiBaoGia.ToString();
                    cboLoaiTepTin.SelectedValue = entBaoGia.IdLoaiTepTin.ToString();
                    chkTrangThai.Checked = entBaoGia.TrangThai;
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMBaoGia.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
            }
        }
        if (e.CommandName == "Delete")
        {
            try
            {
                int IdDelete = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.BaoGiaProvider.Delete(IdDelete);
                if (!deleteStatus)
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.BAOGIA_XOA_THATBAI, true);
                else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.BAOGIA_XOA_THANHCONG, true);
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMBaoGia.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.BAOGIA_XOA_THATBAI, true);
            }
        }
    }

    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Pager1.CurrentIndex = 1;
        Bind_Data(Pager1.CurrentIndex);
        txtBaoGia.Text = "";
        lblFileDinhKem.Text = "";
        cboLoaiBaoGia.SelectedValue = "0";
        cboLoaiTepTin.SelectedValue = "0";
        H_CatalogId.Value = "0";
    }

    public string GetLoaiBaoGia(object Char)
    {
        int idBaoGia = Convert.ToInt32(Char.ToString());
        using (LoaiBaoGia entBaoGia = DataRepository.LoaiBaoGiaProvider.GetById(idBaoGia))
        {
            return entBaoGia.TenLoaiBaoGia;
        }
    }

    public string GetLoaiTepTin(object Char)
    {
        int idTepTin = Convert.ToInt32(Char.ToString());
        using (LoaiTepTin entTepTin = DataRepository.LoaiTepTinProvider.GetById(idTepTin))
        {
            return entTepTin.LoaiTepTin;
        }
    }

    //--------------------------------
    protected void UploadFile_FileUploaded(object sender, CuteWebUI.UploaderEventArgs args)
    {
        string sBaoGiaPath = Server.MapPath(Common.sPathUpBaoGia);
        if (!Directory.Exists(sBaoGiaPath)) Directory.CreateDirectory(sBaoGiaPath);
        if (File.Exists(sBaoGiaPath + "\\" + args.FileName))
        {
            File.Delete(sBaoGiaPath + "\\" + args.FileName);
        }
        if (File.Exists(sBaoGiaPath + "\\" + H_FileChonLai.Value))
        {
            File.Delete(sBaoGiaPath + "\\" + H_FileChonLai.Value);
        }
        args.MoveTo(sBaoGiaPath + "\\" + args.FileName);
        H_FileName.Value = args.FileName;
        lblFileDinhKem.Text = args.FileName;
    }

    protected void UploadFile_AttachmentRemoveClicked(object sender, CuteWebUI.AttachmentItemEventArgs args)
    {
        string sBaoGiaPath = Server.MapPath(Common.sPathUpBaoGia);
        if (File.Exists(sBaoGiaPath + "\\" + args.Item.FileName))
        {
            File.Delete(sBaoGiaPath + "\\" + args.Item.FileName);
        }
        H_FileName.Value = "";
    }

}